package com.htu.controller;

import com.htu.constant.CResultType;
import com.htu.domain.Order;
import com.htu.domain.Result;
import com.htu.model.IOrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController
{
    @Autowired
    private IOrderModel model;

    @PostMapping("/order/insert")
    public Result<Boolean> doOrderInsert(Order order)
    {
        boolean orderInsertState = model.doOrderInsert(order);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (orderInsertState)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, orderInsertState);
    }

    @PostMapping("/order/query")
    public Result<List<Order>> doOrderQueryAll()
    {
        List<Order> orders = model.doOrderQueryAll();
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (orders.size() != 0)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<List<Order>>(resultType, msg, orders);
    }

    @PostMapping("/order/update")
    public Result<Boolean> doOrderUpdate(Order order)
    {
        boolean orderUpdateState = model.doOrderUpdate(order);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (orderUpdateState)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, orderUpdateState);
    }

    @PostMapping("/order/delete")
    public Result<Boolean> doOrderDelete(int orderId)
    {
        boolean orderDeleteState = model.doOrderDelete(orderId);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (orderDeleteState)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, orderDeleteState);
    }
}
